package win;

import constant.Constant;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextField;
import utils.InUtils;
import utils.ReadAndWriteFile;

import java.io.File;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

public class PeiZhiController implements Initializable {
    @FXML
    private TextField p_jiao_xian_chang_du;
    @FXML private TextField p_jiao_xian_ju_bian;
    @FXML private TextField p_jiao_xian_cu_xi;
    @FXML private TextField p_jian_ju;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        if (new File(Constant.file_canshu).exists()) {
            String jiao_xian_chang_du="";
            String jiao_xian_ju_bian="";
            String jiao_xian_cu_xi="";
            String space="";
            List<String> list = ReadAndWriteFile.readtFile(Constant.file_canshu,"utf-8");
            for(String str:list){
                if(str.indexOf("脚线长度")!=-1){
                    jiao_xian_chang_du = str.split("=")[1];
                }else if(str.indexOf("脚线距边")!=-1){
                    jiao_xian_ju_bian = str.split("=")[1];
                }else if(str.indexOf("脚线粗细")!=-1){
                    jiao_xian_cu_xi = str.split("=")[1];
                }else if(str.indexOf("间距")!=-1){
                    space = str.split("=")[1];
                }
            }
            p_jiao_xian_ju_bian.setText(jiao_xian_ju_bian);
            p_jiao_xian_cu_xi.setText(jiao_xian_cu_xi);
            p_jiao_xian_chang_du.setText(jiao_xian_chang_du);
            p_jian_ju.setText(space);

        }

    }

    public void baocun(ActionEvent actionEvent) {
        String space = p_jian_ju.getText();
        String changdu = p_jiao_xian_chang_du.getText();
        String cuxi = p_jiao_xian_cu_xi.getText();
        String jubian = p_jiao_xian_ju_bian.getText();

        /**
         * 脚线长度=3
         * 脚线距边=5
         * 脚线粗细=0.1
         * 间距=4
         */
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("\r\n");
        stringBuilder.append("脚线长度="+changdu+"\r\n");
        stringBuilder.append("脚线距边="+jubian+"\r\n");
        stringBuilder.append("脚线粗细="+cuxi+"\r\n");
        stringBuilder.append("间距="+space+"\r\n");
        ReadAndWriteFile.writeInFile(Constant.file_canshu,stringBuilder.toString(),"utf8");
        InUtils.showMessage(Constant.titile,"保存成功");

    }

    public void quxiao(ActionEvent actionEvent) {
        Constant.peizhiStage.close();
    }
}
